<template>
  <div class="login-container">
    <div class="login-form">
      <h2 class="login-title">RBAC权限管理系统</h2>
      <t-form :data="formData" :rules="rules" @submit="handleLogin">
        <t-form-item name="username" label="用户名">
          <t-input v-model="formData.username" size="large" placeholder="请输入用户名" autocomplete="username">
            <template #prefix-icon>
              <UserIcon />
            </template>
          </t-input>
        </t-form-item>
        
        <t-form-item name="password" label="密码">
          <t-input v-model="formData.password" type="password" size="large" placeholder="请输入密码" autocomplete="current-password">
            <template #prefix-icon>
              <LockOnIcon />
            </template>
          </t-input>
        </t-form-item>
        
        <t-form-item>
          <t-button theme="primary" type="submit" size="large" block :loading="loading">
            登录
          </t-button>
        </t-form-item>
      </t-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { MessagePlugin } from 'tdesign-vue-next'
import { useAuthStore } from '@/stores/auth'
import { UserIcon, LockOnIcon } from 'tdesign-icons-vue-next'

const router = useRouter()
const authStore = useAuthStore()
const loading = ref(false)

const formData = reactive({
  username: '',
  password: ''
})

const rules = {
  username: [{ required: true, message: '请输入用户名', type: 'error' }],
  password: [{ required: true, message: '请输入密码', type: 'error' }]
}

const handleLogin = async () => {
  loading.value = true
  try {
    await authStore.login(formData.username, formData.password)
    MessagePlugin.success('登录成功')
    router.push('/')
  } catch (error: any) {
    MessagePlugin.error(error.response?.data?.error || '登录失败')
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.login-container {
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.login-form {
  width: 400px;
  padding: 40px;
  background: white;
  border-radius: 8px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
}

.login-title {
  text-align: center;
  margin-bottom: 30px;
  color: #333;
  font-size: 24px;
  font-weight: 600;
}

.t-form-item {
  margin-bottom: 20px;
}

.t-input {
  width: 100%;
}
</style>